#include "emotionwindow.h"
#include <QVBoxLayout>
#include <QtMsgHandler>
#include <QDebug>

EmotionWindow::EmotionWindow(QWidget *parent)
    : QWidget(parent)
    , m_normalEmotionWidget(NULL)
{
    setWindowFlags(Qt::FramelessWindowHint | Qt::SubWindow);
    setAttribute(Qt::WA_TranslucentBackground);
    setAttribute(Qt::WA_DeleteOnClose);

    initWindow();
    initEmotion();

}
EmotionWindow::~EmotionWindow()
{}

void EmotionWindow::initWindow()
{

    //this->setStyleSheet("background:rgb(220, 240, 160);");
}

// 初始化表情窗口
void EmotionWindow::initEmotion()
{

    //表情的路径
    QString path = ":/QQexpression/%1.gif";


    // 初始化正常表情框;
    m_normalEmotionWidget = new MyEmotionWidget;
    m_normalEmotionWidget->setRowAndColumn(10, 14);
    m_normalEmotionWidget->setEmotionSize(QSize(32, 32));
    m_normalEmotionWidget->setEmotionMovieSize(QSize(24, 24));
    m_normalEmotionWidget->setMaxRow(6);
    m_normalEmotionWidget->initTableWidget();
    for (int i = 0; i < 132; i++)
    {
        m_normalEmotionWidget->addEmotionItem(path.arg(i + 1),"");
    }
    connect(m_normalEmotionWidget, SIGNAL(signalItemClicked(QString)), this, SLOT(onEmotionItemClicked(QString)));
    // 初始化样式
    m_lableTitle = new QLabel;
    QVBoxLayout* vLayout = new QVBoxLayout;
    vLayout->addWidget(m_lableTitle);
    vLayout->addWidget(m_normalEmotionWidget);
    this->setLayout(vLayout);
}
void EmotionWindow::showEvent(QShowEvent *event)
{
    setFocus();
    QWidget::showEvent(event);
}

void EmotionWindow::focusOutEvent(QFocusEvent *event)
{
    //qDebug()<<"失去焦点";
    /*
    QWidget* widget = qApp->widgetAt(QCursor::pos());
    if (widget != this && widget != ui.emotionWidget && widget != ui.topWidget)
    {
        hide();
        emit signalEmotionWindowHide();
    }
    */
    hide();
    emit signalEmotionWindowHide();
    QWidget::focusOutEvent(event);
}
void EmotionWindow::onEmotionItemClicked(QString filename)
{
    //qDebug()<<"选择"<<emotionNum;
    hide();
    emit signalEmotionWindowHide();
    emit signalEmotionItemClicked(filename);
}
// 显示大表情窗口
void EmotionWindow::showNormalEmotion(QPoint point)
{

    m_lableTitle->setText("This is Normal Emotion Window");
    this->setFixedSize(QSize(m_normalEmotionWidget->width() + 20, m_normalEmotionWidget->height() + 50));
    move(point);
    show();
}
// 重写nativeEvent事件
/*
bool EmotionWindow::nativeEvent(const QByteArray &eventType, void *message, long *result)
{
    MSG* msg = reinterpret_cast<MSG*>(message);
    switch (msg->message)
    {
    case WM_MOUSEMOVE:
    {
        // 将鼠标在EmotionWindow上的坐标传递到表情窗口中
        QPoint point = QCursor::pos() - this->pos();
        m_normalEmotionWidget->showDynamicEmotion(point);
        m_smallEmotionWidget->showDynamicEmotion(point);
    }
    }
    return QWidget::nativeEvent(eventType, message, result);
}*/
